package com.qf.fmall.user.controller;

import com.qf.fmall.user.service.IUserService;
import com.qf.fmall.vo.ResultVo;
import com.qf.fmall.vo.UserRegistVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author leon
 * @since 2024-01-15
 */
@CrossOrigin //跨域
@RestController
@RequestMapping("/user")
@Slf4j  // 打印日志的注解
public class UserController {
    @Autowired
    private IUserService userService;

    @RequestMapping("/login")
    public ResultVo login(String username,String password){
        log.info("登录接口接收到参数,username={},password={}",username,password);
        if (username==null||"".equals(username)){
            log.info("用户名没有传");
            return new ResultVo(-1, "用户名没有传", null);
        }
        if (password==null||"".equals(password)){
            log.info("密码名没有传");
            return new ResultVo(-1, "密码名没有传", null);
        }

        // 调service
        ResultVo resultVo = userService.login(username,password);
        return resultVo;
    }

    @RequestMapping("/regist")
    public ResultVo regist(@RequestBody UserRegistVo userRegistVo){
        // 打印接口传入的参数
        log.info("接收到的注册用户信息为：userRegistVo={}",userRegistVo);

        // 将注册逻辑交给 service去处理
        boolean flag =  userService.regist(userRegistVo);

        if (flag){
            return new ResultVo(10000, "ok", null);
        }else {
            return new ResultVo(-10000, "error", null);
        }

    }
}
